package com.monitor.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.DataUtil;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售维修表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@RestController
@RequestMapping("/erpSaleRepairOrder")
@Api(description="销售维修表")
public class ErpSaleRepairOrderController {

    @Resource
    private ErpMaterielFlowSummaryService flowSummaryService;
    @Resource
    private ErpSaleRepairGarageService saleRepairGarageService;
    @Resource
    private ErpSaleRepairRepairService saleRepairRepairService;
    @Resource
    private ErpSaleRepairOrderService repairOrderService;
    @Resource
    private ErpUserService userService;
    @Resource
    private ErpSaleRepairSaleService repairSaleService;
    @Resource
    private ErpCustomerService customerService;
    @Resource
    private ErpCustomerWarehouseService customerWarehouseService;
    @Resource
    private ErpMaterielFlowService materielFlowService;
    @Resource
    private ErpMaterielService materielService;
    @Resource
    private ErpMaterielFlowMaterielDetailsService detailsService;
    @Resource
    private ErpWarehouseService warehouseService;
    @Resource
    private ErpMaterielFlowMaterielService materielFlowMaterielService;
    @Resource
    private ErpManagementService managementService;
    @Autowired
    private ErpTpmsTireInformationService erpTpmsTireInformationService;
    @Autowired
    private ErpReportService erpReportService;
    @Autowired
    private ErpSaleRepairSaleService erpSaleRepairSaleService;

    @Autowired
    private  ErpHcService erpHcService;
    @RequestMapping("/updateTire")
    @ApiOperation("修改轮胎归属状态")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateSaleRepair(Integer custId,String tireCode,Double price ) {
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpCustomerWarehouse goCustomerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",custId));//入库客户仓库
            ErpMateriel materiel=materielService.selectOne(new EntityWrapper<ErpMateriel>().eq("materiel_production_id",tireCode));
            materiel.setWarehouseId(goCustomerWarehouse.getId());
            materiel.setMaterielStoragePrice(price);

            result.setData(materielService.updateById(materiel)?1:0);
        }catch (Exception e){e.printStackTrace();}
            return result;

    }




    @RequestMapping("/inertOrUpdateSaleRepair")
    @ApiOperation("添加OR修改销售维修")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String,Object>> inertOrUpdateSaleRepair(Integer custId,ErpSaleRepairOrder repairOrder,String tireCode /*, List<ErpSaleRepairGarage> repairGarages, List<ErpSaleRepairRepair> saleRepairRepairs, List<ErpSaleRepairSale> saleRepairSales*/) {
        Result<Map<String,Object>>  result=new Result<Map<String,Object>>();
        try{

            Map<String,Object> map=new HashMap<String, Object>();

            //添加销售维修表数据
            ErpUser user=userService.selectOne(new EntityWrapper<ErpUser>().eq("customer_id",repairOrder.getCustomerId()));
            ErpCustomer customer=customerService.selectById(repairOrder.getCustomerId());//出库客户信息
            ErpCustomer goCustomer=customerService.selectById(custId);//入库客户信息

            repairOrder.setUserId(user.getId());
            //repairOrder.setSaleRepairDate(new Date());
            // repairOrder.setSaleRepairSheetId(IDUtils.genOrderId());
            repairOrderService.insert(repairOrder);

//工作单添加
            ErpManagement management=new ErpManagement();
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            management.setDt(dateString);
            management.setWorkId(repairOrder.getSaleRepairSheetId());
            management.setClientName(repairOrder.getCustomerName());
            management.setWorkProject("被动出库");
            management.setSail(repairOrder.getSaleRepairCost());
            management.setCustomerId(customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_name",repairOrder.getCustomerName())).getId());
            management.setAccounts(repairOrder.getSaleRepairCost());
            //此处应该生成收款单
            management.setWorkPlace(customer.getCusName());
            managementService.insert(management);


            ErpCustomerWarehouse customerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",repairOrder.getCustomerId()));//出库客户仓库
            ErpCustomerWarehouse goCustomerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",custId));//入库客户仓库

            //添加物料流转表信息
            ErpMaterielFlow materielFlow =new ErpMaterielFlow();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd");
            String format = simpleDateFormat.format(new Date());
            materielFlow.setMaterielFlowDate(format);
            //查到订单最后一个并截取字符串然后五位然后在添加的时候+1
            List<ErpMaterielFlow> erpMaterielFlows = materielFlowService.selectList(new EntityWrapper<ErpMaterielFlow>());
            String substring = erpMaterielFlows.get(erpMaterielFlows.size() - 1).getMaterielFlowSheetId().substring(erpMaterielFlows.get(erpMaterielFlows.size() - 1).getMaterielFlowSheetId().length() - 5);
            materielFlow.setMaterielFlowSheetId("WLLZ"+IDUtils.genOrderId()+IDUtils.addOne(substring));


            materielFlow.setSourceWarehouseId(customerWarehouse.getId());
            materielFlow.setSourceWarehouseName(customerWarehouse.getWarName());
            materielFlow.setWarehouseId(goCustomerWarehouse.getId());
            materielFlow.setWarehouseName(goCustomerWarehouse.getWarName());
            materielFlow.setFlowDirection(1);//1出库
            materielFlow.setTheId(custId);
            materielFlowService.insertOrUpdate(materielFlow);


//            //批量添加那仨
//            if (null!=repairOrder.getRepairGarages()){
//                saleRepairGarageService.insertBatch(repairOrder.getRepairGarages());
//            }
//            if (null!=repairOrder.getSaleRepairRepairs())
//            saleRepairRepairService.insertBatch(repairOrder.getSaleRepairRepairs());
//            if (null!=repairOrder.getSaleRepairSales())
//            erpSaleRepairSaleService.insertBatch(repairOrder.getSaleRepairSales());

            map.put("materielFlow",materielFlow);
            map.put("repairOrder",repairOrder);


            //List<ErpMateriel> materielList=new ArrayList<ErpMateriel>();
            //0添加销售信息
            /*for (ErpSaleRepairSale saleRepairSale:saleRepairSales) {
                //添加销售信息
                saleRepairSale.setOrderId(repairOrder.getId());
                saleRepairSale.setWarehouseId(customerWarehouse.getId());
                repairSaleService.insert(saleRepairSale);

                //先查询物料属性信息
                ErpMaterielFlowMateriel materielFlowMateriel=materielFlowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",saleRepairSale.getMaterielName()).eq("materiel_brand",saleRepairSale.getMaterielBrand()).eq("materiel_Specifications",saleRepairSale.getMaterielSpecifications()).eq("materiel_model",saleRepairSale.getMaterielModel()).eq("materiel_place",saleRepairSale.getMaterielPlace()).eq("materiel_state",saleRepairSale.getMaterielState()).eq("materiel_unit",saleRepairSale.getMaterielUnit()));
                if(null==materielFlowMateriel){
                    materielFlowMateriel.setMaterielName(saleRepairSale.getMaterielName());
                    materielFlowMateriel.setMaterielBrand(saleRepairSale.getMaterielBrand());
                    materielFlowMateriel.setMaterielSpecifications(saleRepairSale.getMaterielSpecifications());
                    materielFlowMateriel.setMaterielModel(saleRepairSale.getMaterielModel());
                    materielFlowMateriel.setMaterielPlace(saleRepairSale.getMaterielPlace());
                    materielFlowMateriel.setMaterielState(saleRepairSale.getMaterielState());
                    materielFlowMateriel.setMaterielUnit(saleRepairSale.getMaterielUnit());
                    materielFlowMaterielService.insert(materielFlowMateriel);
                }


                //添加流转的物料得属性信息
                ErpMateriel materiel=new ErpMateriel();
                materiel.setMaterielId(materielFlowMateriel.getId());
                materiel.setMaterielFlowId(materielFlow.getId());
                materiel.setMaterielNumber(saleRepairSale.getMaterielNumber());
                materielService.insertOrUpdate(materiel);

                materielList.add(materiel);

            }*/
//0添加轮胎维修表
            /*for (ErpSaleRepairRepair saleRepairRepair:saleRepairRepairs) {
                //添加销售信息
                saleRepairRepair.setOrderId(repairOrder.getId());
                saleRepairRepairService.insert(saleRepairRepair);
                //0添加轮胎维修表
                //先查询物料属性信息
                ErpMaterielFlowMateriel materielFlowMateriel=materielFlowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",saleRepairRepair.getMaterielName()).eq("materiel_brand",saleRepairRepair.getMaterielBrand()).eq("materiel_Specifications",saleRepairRepair.getMaterielSpecifications()).eq("materiel_model",saleRepairRepair.getMaterielModel()).eq("materiel_place",saleRepairRepair.getMaterielPlace()).eq("materiel_state",saleRepairRepair.getMaterielState()).eq("materiel_unit",saleRepairRepair.getMaterielUnit()));
                if(null==materielFlowMateriel){
                    materielFlowMateriel.setMaterielName(saleRepairRepair.getMaterielName());
                    materielFlowMateriel.setMaterielBrand(saleRepairRepair.getMaterielBrand());
                    materielFlowMateriel.setMaterielSpecifications(saleRepairRepair.getMaterielSpecifications());
                    materielFlowMateriel.setMaterielModel(saleRepairRepair.getMaterielModel());
                    materielFlowMateriel.setMaterielPlace(saleRepairRepair.getMaterielPlace());
                    materielFlowMateriel.setMaterielState(saleRepairRepair.getMaterielState());
                    materielFlowMateriel.setMaterielUnit(saleRepairRepair.getMaterielUnit());
                    materielFlowMaterielService.insert(materielFlowMateriel);
                }
                //添加流转的物料得属性信息
                ErpMateriel materiel=new ErpMateriel();
                materiel.setMaterielId(materielFlowMateriel.getId());
                materiel.setMaterielFlowId(materielFlow.getId());
                materiel.setMaterielNumber(saleRepairRepair.getMaterielNumber());
                materielService.insertOrUpdate(materiel);

                saleRepairRepair.setMaterieId(materielFlowMateriel.getId());
                materielList.add(materiel);
            }*/
//0添加车辆维修表
            /*for (ErpSaleRepairGarage saleRepairGarage:repairGarages) {
                //添加销售信息
                saleRepairGarage.setOrderId(repairOrder.getId());
                saleRepairGarageService.insert(saleRepairGarage);
            }*/


            //1添加流转的物料得属性信息          2 计算出库
            /*for (ErpMateriel materiel:materielList) {
                //计算出库
                *//*******物料出库**********//*
                ErpWarehouse warehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",customerWarehouse.getId()).eq("customer_id",customer.getId()).eq("materiel_id",materiel.getMaterielId()));
                Integer num=warehouse.getMaterielNumber();
                warehouse.setMaterielNumber(num-materiel.getMaterielNumber());
                warehouseService.updateById(warehouse);//修改仓库物料数量

                *//*****物料入库**********//*
                ErpWarehouse goWarehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",goCustomerWarehouse.getId()).eq("customer_id",goCustomer.getId()).eq("materiel_id",materiel.getMaterielId()));
                if(null==goWarehouse){
                    goWarehouse.setCustomerId(goCustomer.getId());
                    goWarehouse.setWarehouseId(goCustomerWarehouse.getId());
                    goWarehouse.setMaterielId(materiel.getMaterielId());
                    warehouseService.insert(goWarehouse);
                }
                goWarehouse.setMaterielNumber(goWarehouse.getMaterielNumber()+materiel.getMaterielNumber());
                warehouseService.updateById(goWarehouse);

                //添加物料流转物料详情
                ErpMaterielFlowMaterielDetails materielFlowMaterielDetails=new ErpMaterielFlowMaterielDetails();
                materielFlowMaterielDetails.setMaterielId(materiel.getId());
                materielFlowMaterielDetails.setPid(materielFlow.getId());
                materielFlowMaterielDetails.setMaterielStudy(0);
                detailsService.insert(materielFlowMaterielDetails);

                //添加物料流转统计信息
                ErpMaterielFlowSummary materielFlowSummary=new ErpMaterielFlowSummary();
                materielFlowSummary.setWarehouseId(goCustomerWarehouse.getId());
                materielFlowSummary.setMaterielId(materiel.getMaterielId());
                materielFlowSummary.setWorkSheetId(materielFlow.getMaterielFlowSheetId());
                materielFlowSummary.setMaterielFlowDate(DataUtil.getStringDate());
                materielFlowSummary.setMaterielStorageNumber(materiel.getMaterielNumber());
                materielFlowSummary.setMaterielStoragePrice(materiel.getMaterielStoragePrice());
                materielFlowSummary.setMaterielOutflowNumber(materiel.getMaterielNumber());
                materielFlowSummary.setMaterielOutflowPrice(materiel.getMaterielStoragePrice());
                //materielFlowSummary.setIncludingTax();
                materielFlowSummary.setMaterielFlowType(1);
                materielFlowSummary.setMaterielRetained(goWarehouse.getMaterielNumber());
                materielFlowSummary.setOutWarehouseRetauned(num);  // 数据库里没看见
                materielFlowSummary.setMaterielSource(customer.getCusName());
                materielFlowSummary.setMaterielWhereabouts(goCustomer.getCusName());
                materielFlowSummary.setOutWarehouseId(customerWarehouse.getId());
                flowSummaryService.insert(materielFlowSummary);
            }*/

            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/inertSaleRepairSale")
    @ApiOperation("添加销售表")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "flowId", value = "物料流转单ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "flowSheet", value = "物料流转单单号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "saleRepairId", value = "销售维修ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<Integer> inertSaleRepairSale(Integer custId,ErpSaleRepairSale saleRepairSale,Integer saleRepairId,Integer flowId,String flowSheet) {
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpSaleRepairOrder repairOrder=repairOrderService.selectById(saleRepairId);
            ErpUser user=userService.selectOne(new EntityWrapper<ErpUser>().eq("customer_id",repairOrder.getCustomerId()));
            ErpCustomer customer=customerService.selectById(repairOrder.getCustomerId());//出库客户信息
            ErpCustomer goCustomer=customerService.selectById(custId);//入库客户信息

            ErpCustomerWarehouse customerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",repairOrder.getCustomerId()));//出库客户仓库
            ErpCustomerWarehouse goCustomerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",custId));//入库客户仓库


            //0添加轮胎维修表
            //先查询物料属性信息
            saleRepairSale.setOrderId(repairOrder.getSaleRepairSheetId());
            saleRepairSale.setWarehouseId(customerWarehouse.getId());
            repairSaleService.insert(saleRepairSale);

            //先查询物料属性信息
            ErpMaterielFlowMateriel materielFlowMateriel=new ErpMaterielFlowMateriel();
            materielFlowMateriel=materielFlowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",saleRepairSale.getMaterielName()).eq("materiel_brand",saleRepairSale.getMaterielBrand()).eq("materiel_Specifications",saleRepairSale.getMaterielSpecifications()).eq("materiel_model",saleRepairSale.getMaterielModel()).eq("materiel_place",saleRepairSale.getMaterielPlace()).eq("materiel_state",saleRepairSale.getMaterielState()).eq("materiel_unit",saleRepairSale.getMaterielUnit()));
            if(null==materielFlowMateriel){
                materielFlowMateriel=new ErpMaterielFlowMateriel();
                materielFlowMateriel.setMaterielName(saleRepairSale.getMaterielName());
                materielFlowMateriel.setMaterielBrand(saleRepairSale.getMaterielBrand());
                materielFlowMateriel.setMaterielSpecifications(saleRepairSale.getMaterielSpecifications());
                materielFlowMateriel.setMaterielModel(saleRepairSale.getMaterielModel());
                materielFlowMateriel.setMaterielPlace(saleRepairSale.getMaterielPlace());
                materielFlowMateriel.setMaterielState(saleRepairSale.getMaterielState());
                materielFlowMateriel.setMaterielUnit(saleRepairSale.getMaterielUnit());
                materielFlowMaterielService.insert(materielFlowMateriel);
            }


            //添加流转的物料得属性信息
            ErpMateriel materiel=new ErpMateriel();
            materiel.setMaterielId(materielFlowMateriel.getId());
            materiel.setMaterielFlowId(flowId);
            materiel.setMaterielNumber(saleRepairSale.getMaterielNumber());
            materiel.setWarehouseId(customer.getId());
            materielService.insertOrUpdate(materiel);


            ErpWarehouse warehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",customerWarehouse.getId()).eq("customer_id",customer.getId()).eq("materiel_id",materiel.getMaterielId()));
            Integer num=warehouse.getMaterielNumber();
            warehouse.setMaterielNumber(num-materiel.getMaterielNumber());
            warehouseService.updateById(warehouse);//修改仓库物料数量

            /*****物料入库**********/
            ErpWarehouse goWarehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",goCustomerWarehouse.getId()).eq("customer_id",goCustomer.getId()).eq("materiel_id",materiel.getMaterielId()));
            if(null==goWarehouse){
                goWarehouse.setCustomerId(goCustomer.getId());
                goWarehouse.setWarehouseId(goCustomerWarehouse.getId());
                goWarehouse.setMaterielId(materiel.getMaterielId());
                warehouseService.insert(goWarehouse);
            }
            goWarehouse.setMaterielNumber(goWarehouse.getMaterielNumber()+materiel.getMaterielNumber());
            warehouseService.updateById(goWarehouse);

            //添加物料流转物料详情
            ErpMaterielFlowMaterielDetails materielFlowMaterielDetails=new ErpMaterielFlowMaterielDetails();
            materielFlowMaterielDetails.setMaterielId(materiel.getId());
            materielFlowMaterielDetails.setPid(flowId);
            materielFlowMaterielDetails.setMaterielStudy(0);
            detailsService.insert(materielFlowMaterielDetails);

            //添加物料流转统计信息
            ErpMaterielFlowSummary materielFlowSummary=new ErpMaterielFlowSummary();
            materielFlowSummary.setWarehouseId(goCustomerWarehouse.getId());
            materielFlowSummary.setMaterielId(materiel.getMaterielId());
            materielFlowSummary.setWorkSheetId(flowSheet);
            materielFlowSummary.setMaterielFlowDate(DataUtil.getStringDate());
            materielFlowSummary.setMaterielStorageNumber(materiel.getMaterielNumber());
            materielFlowSummary.setMaterielStoragePrice(materiel.getMaterielStoragePrice());
            materielFlowSummary.setMaterielOutflowNumber(materiel.getMaterielNumber());
            materielFlowSummary.setMaterielOutflowPrice(materiel.getMaterielStoragePrice());
            //materielFlowSummary.setIncludingTax();
            materielFlowSummary.setMaterielFlowType(1);
            materielFlowSummary.setMaterielRetained(goWarehouse.getMaterielNumber());
            materielFlowSummary.setOutWarehouseRetauned(num);  // 数据库里没看见
            materielFlowSummary.setMaterielSource(customer.getCusName());
            materielFlowSummary.setMaterielWhereabouts(goCustomer.getCusName());
            materielFlowSummary.setOutWarehouseId(customerWarehouse.getId());
            flowSummaryService.insert(materielFlowSummary);


            ErpTpmsTireInformation information=erpTpmsTireInformationService.selectOne(new EntityWrapper<ErpTpmsTireInformation>().eq("brand",saleRepairSale.getMaterielBrand()).and().eq("specifications",saleRepairSale.getMaterielSpecifications()).and().eq("model",saleRepairSale.getMaterielModel()).and().eq("place",saleRepairSale.getMaterielPlace()));
            if(information==null){
                information=new ErpTpmsTireInformation();
                information.setPid(custId);
                information.setBrand(saleRepairSale.getMaterielBrand());
                information.setSpecifications(saleRepairSale.getMaterielSpecifications());
                information.setModel(saleRepairSale.getMaterielModel());
                information.setPlace(saleRepairSale.getMaterielPlace());
                erpTpmsTireInformationService.insert(information);
            }else{
                information.setPid(custId);
                erpTpmsTireInformationService.updateById(information);
            }




            result.setCount(1);
            result.setData(1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }




    @RequestMapping("/inertSaleRepairRepair")
    @ApiOperation("添加销售维修添加轮胎维修信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "flowId", value = "物料流转单ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "flowSheet", value = "物料流转单单号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "saleRepairId", value = "销售维修ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<Integer> inertSaleRepairRepair(Integer custId,ErpSaleRepairRepair saleRepairRepair,Integer saleRepairId,Integer flowId,String flowSheet) {
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpSaleRepairOrder repairOrder=repairOrderService.selectById(saleRepairId);
            ErpUser user=userService.selectOne(new EntityWrapper<ErpUser>().eq("customer_id",repairOrder.getCustomerId()));
            ErpCustomer customer=customerService.selectById(repairOrder.getCustomerId());//出库客户信息
            ErpCustomer goCustomer=customerService.selectById(custId);//入库客户信息

            ErpCustomerWarehouse customerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",repairOrder.getCustomerId()));//出库客户仓库
            ErpCustomerWarehouse goCustomerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",custId));//入库客户仓库
            saleRepairRepair.setOrderId(repairOrder.getSaleRepairSheetId());
            saleRepairRepairService.insert(saleRepairRepair);
            //0添加轮胎维修表
            //先查询物料属性信息
            ErpMaterielFlowMateriel materielFlowMateriel=materielFlowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",saleRepairRepair.getMaterielName()).eq("materiel_brand",saleRepairRepair.getMaterielBrand()).eq("materiel_Specifications",saleRepairRepair.getMaterielSpecifications()).eq("materiel_model",saleRepairRepair.getMaterielModel()).eq("materiel_place",saleRepairRepair.getMaterielPlace()).eq("materiel_state",saleRepairRepair.getMaterielState()).eq("materiel_unit",saleRepairRepair.getMaterielUnit()));
            if(null==materielFlowMateriel){
                materielFlowMateriel=new ErpMaterielFlowMateriel();
                materielFlowMateriel.setMaterielName(saleRepairRepair.getMaterielName());
                materielFlowMateriel.setMaterielBrand(saleRepairRepair.getMaterielBrand());
                materielFlowMateriel.setMaterielSpecifications(saleRepairRepair.getMaterielSpecifications());
                materielFlowMateriel.setMaterielModel(saleRepairRepair.getMaterielModel());
                materielFlowMateriel.setMaterielPlace(saleRepairRepair.getMaterielPlace());
                materielFlowMateriel.setMaterielState(saleRepairRepair.getMaterielState());
                materielFlowMateriel.setMaterielUnit(saleRepairRepair.getMaterielUnit());
                materielFlowMaterielService.insert(materielFlowMateriel);
            }
            //添加流转的物料得属性信息
            ErpMateriel materiel=new ErpMateriel();
            materiel.setMaterielId(materielFlowMateriel.getId());
            materiel.setMaterielFlowId(flowId);
            materiel.setMaterielNumber(saleRepairRepair.getMaterielNumber());
            materielService.insertOrUpdate(materiel);

            saleRepairRepair.setMaterieId(materielFlowMateriel.getId());


            ErpWarehouse warehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",customerWarehouse.getId()).eq("customer_id",customer.getId()).eq("materiel_id",materiel.getMaterielId()));
            Integer num=warehouse.getMaterielNumber();
            warehouse.setMaterielNumber(num-materiel.getMaterielNumber());
            warehouseService.updateById(warehouse);//修改仓库物料数量

            /*****物料入库**********/
            ErpWarehouse goWarehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",goCustomerWarehouse.getId()).eq("customer_id",goCustomer.getId()).eq("materiel_id",materiel.getMaterielId()));
            if(null==goWarehouse){
                goWarehouse.setCustomerId(goCustomer.getId());
                goWarehouse.setWarehouseId(goCustomerWarehouse.getId());
                goWarehouse.setMaterielId(materiel.getMaterielId());
                warehouseService.insert(goWarehouse);
            }
            goWarehouse.setMaterielNumber(goWarehouse.getMaterielNumber()+materiel.getMaterielNumber());
            warehouseService.updateById(goWarehouse);

            //添加物料流转物料详情
            ErpMaterielFlowMaterielDetails materielFlowMaterielDetails=new ErpMaterielFlowMaterielDetails();
            materielFlowMaterielDetails.setMaterielId(materiel.getId());
            materielFlowMaterielDetails.setPid(flowId);
            materielFlowMaterielDetails.setMaterielStudy(0);
            detailsService.insert(materielFlowMaterielDetails);

            //添加物料流转统计信息
            ErpMaterielFlowSummary materielFlowSummary=new ErpMaterielFlowSummary();
            materielFlowSummary.setWarehouseId(goCustomerWarehouse.getId());
            materielFlowSummary.setMaterielId(materiel.getMaterielId());
            materielFlowSummary.setWorkSheetId(flowSheet);
            materielFlowSummary.setMaterielFlowDate(DataUtil.getStringDate());
            materielFlowSummary.setMaterielStorageNumber(materiel.getMaterielNumber());
            materielFlowSummary.setMaterielStoragePrice(materiel.getMaterielStoragePrice());
            materielFlowSummary.setMaterielOutflowNumber(materiel.getMaterielNumber());
            materielFlowSummary.setMaterielOutflowPrice(materiel.getMaterielStoragePrice());
            //materielFlowSummary.setIncludingTax();
            materielFlowSummary.setMaterielFlowType(1);
            materielFlowSummary.setMaterielRetained(goWarehouse.getMaterielNumber());
            materielFlowSummary.setOutWarehouseRetauned(num);  // 数据库里没看见
            materielFlowSummary.setMaterielSource(customer.getCusName());
            materielFlowSummary.setMaterielWhereabouts(goCustomer.getCusName());
            materielFlowSummary.setOutWarehouseId(customerWarehouse.getId());
            flowSummaryService.insert(materielFlowSummary);
            result.setCount(1);
            result.setData(1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }



    @RequestMapping("/inertErpSaleRepairGarage")
    @ApiOperation("  ")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "saleRepairId", value = "销售维修单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> inertErpSaleRepairGarage(ErpSaleRepairGarage repairGarages,String saleRepairId) {
        Result<Integer>  result=new Result<Integer>();
        try{
            repairGarages.setOrderId(saleRepairId);
            saleRepairGarageService.insert(repairGarages);
            result.setCount(1);
            result.setData(1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/findWeekly")
    @ApiOperation("管理报告-代办列表-按周查询汇总")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "name", value = "客户名称", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "workDtS", value = "开始时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "workDtE", value = "结束时间", required = false, dataType = "String", paramType = "query")
    })
    public Result<List<Weelly>> findWeekly(Integer custId,String name, String workDtS, String workDtE){
    	return repairSaleService.findWeekly(custId, name, workDtS, workDtE);
    }
    
    @RequestMapping("/findMon")
    @ApiOperation("管理报告-代办列表-按月查询汇总")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "workDtS", value = "开始时间", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "workDtE", value = "结束时间", required = false, dataType = "String", paramType = "query")
   })
    public Result<Map<String, Object>>  findMon(Integer custId, String workDtS, String workDtE){
    	return repairOrderService.findMon(custId, workDtS, workDtE);
    }
    
    @RequestMapping("/addReport")
    public Result<Integer> addReport(ErpReport report,String child){
    	List<ErpReportChild> ch=new ArrayList<>();
    	if(child!=null&&!child.equals("")) {
    		ch=JSON.parseArray(child, ErpReportChild.class);
    	}
    	return erpReportService.addReport(report, ch);
    }
    
    @RequestMapping("/selectByReportTime")
    @ApiOperation("管理报告-代办列表-按月查询月报")
    @ApiImplicitParam(name = "time", value = "报告时间", required = false, dataType = "String", paramType = "query")
    public Result<Map<String, Object>> selectByReportTime(String time){
    	return erpReportService.selectByReportTime(time);
    }
}

